package com.sxj.corejava.code08_常用类.Object;

/**
 * @author 石小俊
 * @date 2024年04月02日 19:03
 */
public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
//        getClassTest();
//        equalsTest();
//        hashcodeTest();
//        toStringTest();
//        cloneTest2();
        finalizeTest();
    }

    public static void getClassTest() {
        Person person = new Man();
        System.out.println(person.getClass());

        Person person2 = new WoMan();
        System.out.println(person2.getClass());

        System.out.println(Person.class);
        System.out.println(Man.class);
        System.out.println(WoMan.class);

        if (person.getClass() == new Man().getClass()) {
            Man man = (Man) person;
            man.work();
        }
        if (person2.getClass() == WoMan.class) {
            WoMan woMan = (WoMan) person2;
            woMan.shopping();
        }
    }


    public static void equalsTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        System.out.println(person == person2);
        System.out.println(person.equals(person2));
        String s1 = new String("admin");
        String s2 = new String("admin");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }

    public static void hashcodeTest() {
        Person person = new Person("admin", 18);
        Person person2 = new Person("admin", 18);
        Person person3 = new Person("admin", 20);
        System.out.println(person.equals(person2));
        System.out.println(person.equals(person3));
        System.out.println(person.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person3.hashCode());
        System.out.println(person == person2);
    }

    public static void toStringTest() {
        Person person = new Person("admin", 18);
        System.out.println(person);
    }

    public static void cloneTest() throws CloneNotSupportedException {
        Person person = new Person("admin", 20);
        Person person2 = (Person) person.clone();
        Person person3 = person;
        System.out.println(person2);
        System.out.println(person == person2);
        System.out.println(person == person3);
    }

    public static void cloneTest2() throws CloneNotSupportedException {
        Address address = new Address("江苏", "南京");
        Person person = new Person("admin", 20, address);
        System.out.println("person:" + person);
        Object obj = person.clone();
        if (obj instanceof Person) {
            Person person2 = (Person) obj;
            System.out.println("person2:" + person2);

            System.out.println("*****************开始修改*****************");
            person2.setName("jack");
            person2.setAge(18);
            person2.getAddress().setProvince("浙江");
            person2.getAddress().setCity("杭州");

            System.out.println("person:" + person);
            System.out.println("person2:" + person2);
        }
    }

    public static void finalizeTest(){
        Person person = new Person();
        Person person2 = person;
        person = null;
        person2 = null;

        // 建议进行垃圾回收
//        System.gc();
        Runtime.getRuntime().gc();
    }
}
